#include "http.h"

http::http(QObject *parent) : QObject(parent)
{

}
bool http::post_sync(QString Url,QMap<QString,QString>header,QByteArray requestData,QByteArray &replyData)
{
       //发送请求对象
       QNetworkAccessManager manager;
       //请求对象
       QNetworkRequest request;

       request.setUrl(Url);
       QMapIterator<QString,QString>it(header);
       while (it.hasNext()) {      //遍历map
           it.next();
           request.setRawHeader(it.key().toLatin1(),it.value().toLatin1());

       }
      QNetworkReply *reply = manager.post(request,requestData);
      QEventLoop loop;
      //一旦服务器返回，reply会发出信号
      connect(reply,&QNetworkReply::finished,&loop,&QEventLoop::quit);
      loop.exec();
      if(reply !=nullptr && reply->error()== QNetworkReply::NoError)
      {
          replyData = reply->readAll();
          //qDebug() << replyData;
          return true;
      }
      else
      {
          qDebug() << "请求失败！";
          return false;
      }
}
